/**
 * 
 * 
 *  
 *====================================================
 * 文件名称: SessionAttributes.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2008-7-2			
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package com.devframe.web;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;WebContext&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --WEB上下文--&lt;/b&gt;
 * </pre>
 * <pre></pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 *   SessionAttributes obj = new SessionAttributes();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class WebContext implements MyContext {
    /**
     * httpSession 上下文
     */
    private HttpSession session;

    /**
     * application上下文
     * 
     */
    private ServletContext application;

    /**
     * request请求上下文
     */
    private HttpServletRequest request;

    public WebContext(HttpServletRequest request) {
        this.request = request;
        this.session = request.getSession();
        this.application = this.session.getServletContext();
    }

    /*
     * public WebContext(Map application,Map session,Map request,Map parameters) {
     * applicationAttributesHolder = application; session = session;
     * requestAttributesHolder = request; parameters = parameters; }
     */

    public Enumeration getSessionAttributeNames() {

        return session.getAttributeNames();
        // return Collections.enumeration(((Map)session).keySet());
    }

    /**
     * @param name name
     * @return Object
     * 
     */
    public Object getSessionAttribute(String name) {
        return session.getAttribute(name);
    }

    /**
     * @param name name
     * 
     */
    public void removeSessionAttribute(String name) {
        session.removeAttribute(name);
    }

    /**
     * @param name name
     * @return name
     * 
     */
    public String getParameter(String name) {
        return request.getParameter(name);
    }

    /**
     * @return Enumeration
     * 
     */
    public Enumeration getParameterNames() {
        return request.getParameterNames();
    }

    /**
     * @param name name
     * @return String[] 
     * 
     */
    public String[] getParameterValues(String name) {
        return request.getParameterValues(name);
    }

    /**
     * @param name attribName
     * @return Object
     * 
     */
    public Object getRequestAttribute(String name) {
        return request.getAttribute(name);
    }

    /**
     * @return Enumeration
     * 
     */
    public Enumeration getRequestAttributeNames() {
        return request.getAttributeNames();
    }

    /**
     * @param name key
     * 
     */
    public void removeRequestAttribute(String name) {
        request.removeAttribute(name);
    }

    /**
     * @param name key
     * @param o value
     * 
     */
    public void setRequestAttribute(String name, Object o) {
        request.setAttribute(name, o);
    }

    /**
     * @param name key
     * @param value value
     * 
     */
    public void setSessionAttribute(String name, Object value) {
        session.setAttribute(name, value);
    }

    /**
     * @param name name
     * @return Object
     * 
     */
    public Object getApplicationAttribute(String name) {
        
        return application.getAttribute(name);
    }

    /**
     * 
     * @return Enumeration
     * 
     */
    public Enumeration getApplicationAttributeNames() {
        return application.getInitParameterNames();
    }

    /**
     * @param name name
     * 
     */
    public void removeApplicationAttribute(String name) {
        application.removeAttribute(name);
    }

    /**
     * @param name name
     * @param o value
     * 
     */
    public void setApplicationAttribute(String name, Object o) {
        application.setAttribute(name, o);
    }

    /**
     * @return ip地址
     * 
     */
    public String getRemoteAddr() {
    
    	return request.getRemoteAddr();
       
    }

    /**
     * @return 客户端主机
     * 
     */
    public String getRemoteHost() {
        return request.getRemoteHost();
    }

    /**
     * getHttpSession
     * @return  HttpSession
     * 
     */
    public HttpSession getHttpSession() {
        return session;
    }

    /**
     * 获取WebRoot的文件目录路径，以/结尾
     * @return 绝对路径
     * 
     */
    public String getRootPath() {
        try {
            //String rootPath =request.getSession().getServletContext().getResource("/").getFile();
            //String rootPath = this.getClass().getClassLoader().getResource("").getFile();
//            rootPath = rootPath.replaceFirst("/", "");
//            rootPath = rootPath.replaceFirst("WEB-INF/classes/", "");
        	String rootPath = request.getSession().getServletContext().getRealPath("/");
        	rootPath = rootPath.replaceAll("\\\\", "/");
        	rootPath += "/";
        	
            return rootPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param name
     * @return
     * 
     */
    public String[] getUpdateProperties(String name) {
        Enumeration pNames = request.getParameterNames();
        List<String> updateProps=new ArrayList<String>();
        while (pNames.hasMoreElements()){
            String pName = (String) pNames.nextElement();
            if(pName.startsWith(name+".")){
                updateProps.add(pName.substring(name.length()+1));
            }
        }
        String[] props = new String[updateProps.size()];
        updateProps.toArray(props);
        return props;
    }

}
